home *** CD-ROM | disk | FTP | other *** search
/ Technotools / Technotools (Chestnut CD-ROM)(1993).ISO / os2tools / bnklysrc / mdm_proc.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-03-04  |  18.9 KB  |  487 lines

  1. /*--------------------------------------------------------------------------*/
  2. /*                                                                          */
  3. /*                                                                          */
  4. /*      ------------         Bit-Bucket Software <no-Inc>                   */
  5. /*      \ 10001101 /         Writers and Distributors of                    */
  6. /*       \ 011110 /          No-Cost<no-tm> Software.                       */
  7. /*        \ 1011 /                                                          */
  8. /*         ------                                                           */
  9. /*                                                                          */
  10. /*  Copyright (C) 1987, 1988, 1989 by Robert Hartman and Vincent Perriello  */
  11. /*                                                                          */
  12. /*                                                                          */
  13. /*               This module was written by Vince Perriello                 */
  14. /*                                                                          */
  15. /*                                                                          */
  16. /*                    BinkleyTerm Modem Handler Module                      */
  17. /*                                                                          */
  18. /*                                                                          */
  19. /*    For complete  details  of the licensing restrictions, please refer    */
  20. /*    to the License  agreement,  which  is published in its entirety in    */
  21. /*    the MAKEFILE and BT.C, and also contained in the file LICENSE.210.    */
  22. /*                                                                          */
  23. /*    USE  OF THIS FILE IS SUBJECT TO THE  RESTRICTIONS CONTAINED IN THE    */
  24. /*    BINKLEYTERM  LICENSING  AGREEMENT.  IF YOU DO NOT FIND THE TEXT OF    */
  25. /*    THIS  AGREEMENT IN ANY OF THE  AFOREMENTIONED FILES,  OR IF YOU DO    */
  26. /*    NOT HAVE THESE FILES,  YOU SHOULD  IMMEDIATELY CONTACT THE AUTHORS    */
  27. /*    AT THE  ADDRESSES LISTED BELOW.  IN NO EVENT SHOULD YOU PROCEED TO    */
  28. /*    USE   THIS  FILE  WITHOUT  HAVING   ACCEPTED  THE  TERMS  OF   THE    */
  29. /*    BINKLEYTERM  LICENSING AGREEMENT,  OR SUCH OTHER  AGREEMENT AS YOU    */
  30. /*    ARE ABLE TO REACH WITH THE AUTHORS.                                   */
  31. /*                                                                          */
  32. /*                                                                          */
  33. /*    The Authors can be reached at the following addresses:                */
  34. /*                                                                          */
  35. /*    Robert C. Hartman                      Vincent E. Perriello           */
  36. /*    Spark Software                         VEP Software                   */
  37. /*    427-3 Amherst Street                   111 Carroll Street             */
  38. /*    CS2032, Suite 232                      Naugatuck, CT 06770            */
  39. /*    Nashua, NH 03061                                                      */
  40. /*                                                                          */
  41. /*    FidoNet 1:132/101                      FidoNet 1:141/491              */
  42. /*    Data    (603) 888-8179                 Data    (203) 729-7569         */
  43. /*                                                                          */
  44. /*    Please feel free to contact us at any time to share your comments     */
  45. /*    about our software and/or licensing policies.                         */
  46. /*                                                                          */
  47. /*--------------------------------------------------------------------------*/
  48.  
  49. #include <stdio.h>
  50. #include <signal.h>
  51. #include <ctype.h>
  52. #include <conio.h>
  53. #include <string.h>
  54. #include <stdlib.h>
  55.  
  56. #include "com.h"
  57. #include "xfer.h"
  58. #include "zmodem.h"
  59. #include "keybd.h"
  60. #include "sbuf.h"
  61. #include "sched.h"
  62. #include "externs.h"
  63. #include "prototyp.h"
  64.  
  65. static int dial_modem (char *);
  66. static void phone_translate (char *, char *);
  67. static char *get_response (long);
  68. static int parse_response (char *);
  69. static void empty_delay (void);
  70.  
  71. #define FAILURE 0
  72. #define IGNORE    1
  73. #define SUCCESS 2
  74.  
  75. struct resp_str
  76. {
  77.    char *resp;
  78.    unsigned disp;
  79. };
  80.  
  81. static struct resp_str mdm_resps[] = {
  82.                                       {"RINGING", 1},
  83.                                       {"RING", 3},
  84.                                       {"CONNECT", 2},
  85.                                       {"RRING", 1},
  86.                                       {"BUSY", 0},
  87.                                       {"VOICE", 0},
  88.                                       {"ERROR", 0},
  89.                                       {"OK", 0},
  90.                                       {"NO CARRIER", 0},
  91. /* "NO DIAL TONE" or "NO DIALTONE" */ {"NO DIAL", 0}, 
  92.                                       {"DIAL TONE", 0},
  93.                                       {NULL, 0}
  94. };
  95.  
  96. static char *response_string = "                    ";
  97.  
  98. void try_2_connect (phnum)
  99. char *phnum;
  100. {
  101.    long t1, timerset ();
  102.    int j, k;
  103.  
  104.    for (j = 0; (j < poll_tries && !KEYPRESS ()); j++) /* do polltries or till keypress */
  105.       {
  106.       CLEAR_INBOUND ();
  107.       k = dial_modem (phnum);
  108.  
  109.       if ((un_attended || doing_poll) && fullscreen)
  110.          {
  111.          ++hist.calls_made;
  112.          sb_move (historywin, HIST_ATT_ROW, HIST_COL);
  113.          sprintf (junk, "%-4d", hist.calls_made);
  114.          sb_puts (historywin, junk);
  115.          }
  116.  
  117.       if ((k > 0) || KEYPRESS ())
  118.          break;
  119.       t1 = timerset (200);
  120.       while (!timeup (t1) && !KEYPRESS ())
  121.          time_release ();                        /* pause for 2 seconds */
  122.       }
  123.    if (KEYPRESS ())                              /* If user's been busy */
  124.       {
  125.       if (!caller)
  126.          FOSSIL_CHAR ();                         /* Eat the character   */
  127.       if (!CARRIER)                              /* Abort if no carrier */
  128.          {
  129.          status_line ("!Connection attempt aborted");
  130.          mdm_hangup ();
  131.          }
  132.       }
  133. }
  134.  
  135. int try_1_connect (phnum)
  136. char *phnum;
  137. {
  138.    int k;
  139.  
  140.    if ((k = dial_modem (phnum)) <= 0)
  141.       {
  142.       mdm_hangup ();
  143.       }
  144.  
  145.    if ((un_attended || doing_poll) && fullscreen)
  146.       {
  147.       ++hist.calls_made;
  148.       sb_move (historywin, HIST_ATT_ROW, HIST_COL);
  149.       sprintf (junk, "%-4d", hist.calls_made);
  150.       sb_puts (historywin, junk);
  151.       }
  152.  
  153.    return (k);
  154. }
  155.  
  156. static void phone_translate (number, translated)
  157. char *number;
  158. char *translated;
  159. {
  160.    PN_TRNS *p;
  161.  
  162.    strcpy (translated, number);
  163.    for (p = pn_head; p != NULL; p = p->next)
  164.       {
  165.       if (strncmp (p->num, number, p->len) == 0)
  166.          {
  167.          sprintf (translated, "%s%s%s", p->pre, &(number[p->len]), p->suf);
  168.          break;
  169.          }
  170.       }
  171. }
  172.  
  173. static int dial_modem (number)
  174. char *number;
  175. {
  176.    int resp;
  177.    long t;
  178.    char translated[50];
  179.    extern long timerset ();
  180.  
  181.    phone_translate (number, translated);
  182.    if (translated[0] == '\"')                    /* If it's a script          */
  183.       return (do_script (translated));           /* then do it that way       */
  184.  
  185.    status_line (":Dialing %s", translated);
  186.    if (un_attended && fullscreen)
  187.       {
  188.       do_ready ("Dialing");
  189.       }
  190.  
  191.    /* First of all, if we have something, don't hang up on the guy! */
  192.    if (!no_collide && CHAR_AVAIL ())
  193.       return (-1);
  194.  
  195.    DTR_OFF ();                                   /* drop DTR to reset modem   */
  196.    timer (20);                                   /* leave it down 2 seconds   */
  197.    DTR_ON ();                                    /* then raise DTR again      */
  198.    timer (5);                                    /* and wait .5 sec for modem */
  199.  
  200.    if (!no_collide && CHAR_AVAIL ())             /* If we have something
  201.                                                   * here, return */
  202.       return (-1);
  203.  
  204.    mdm_cmd_string (predial, 0);                  /* transmit the dial prefix  */
  205.    mdm_cmd_string (translated, 0);               /* then the phone number     */
  206.    mdm_cmd_string (postdial, 0);                 /* finally the dial suffix   */
  207.    if (no_collide)
  208.       CLEAR_INBOUND ();                          /* Throw out all echo to
  209.                                                   * this point  */
  210.    mdm_cmd_char (CR);                            /* terminate the string      */
  211.  
  212.    if (resp = modem_response (7500))             /* we got a good response,   */
  213.       {
  214.       if (resp == 3)                             /* Incoming ring to be
  215.                                                   * processed higher up */
  216.          return (-1);
  217.  
  218.       t = timerset (200);                        /* Wait up to 2 seconds      */
  219.       while (!timeup (t))
  220.          {                                       /* If carrier detect, AND    */
  221.          if ((CHAR_AVAIL ()) && CARRIER)         /* some sign of life, */
  222.             break;                               /* leave early...            */
  223.          }
  224.       return (CARRIER);                          /* Carrier should be on now  */
  225.       }
  226.    return (0);                                   /* no good */
  227. }
  228.  
  229. static char *get_response (end_time)
  230. long end_time;                                   /* timeout parameters        */
  231. {
  232.    char *p = response_string;                    /* points to character cell  */
  233.    char c;                                       /* current modem character   */
  234.    int count = 0;                                /* count of characters       */
  235.  
  236.    while ((count < 20)                           /* until we have 20 chars,   */
  237.           && (!timeup (end_time))                /* or out of time,           */
  238.           && (!KEYPRESS ()))                     /* or user gets impatient    */
  239.       {
  240.       if (!CHAR_AVAIL ())                        /* if nothing ready yet,     */
  241.          {
  242.          time_release ();
  243.          continue;                               /* just process timeouts     */
  244.          }
  245.       c = (char) (MODEM_IN () & 0xff);           /* get a character           */
  246.       if (c == '\r' || c == '\n')                /* if a line ending          */
  247.          {
  248.          if (count != 0)                         /* and we have something,    */
  249.             break;                               /* get out                   */
  250.          else continue;                          /* otherwise just keep going */
  251.          }
  252.       *p++ = c;                                  /* store the character       */
  253.       ++count;                                   /* increment the counter     */
  254.       }
  255.    *p = '\0';                                    /* terminate the new string  */
  256.  
  257.    if (count != 0 && strnicmp (response_string, "AT", 2))
  258.       {
  259.       fancy_str (response_string);               /* make it pretty            */
  260.       status_line ("\#%s", response_string);     /* pop it out on the screen  */
  261.       }
  262.  
  263.    return (response_string);                     /* return the pointer        */
  264. }
  265.  
  266. static int parse_response (response)
  267. char *response;
  268. {
  269.    char *p;                                      /* temp character pointer    */
  270.    register int i;                               /* array pointer             */
  271.  
  272.    for (i = 0; mdm_resps[i].resp != NULL; i++)   /* scan through array        */
  273.       {
  274.       p = mdm_resps[i].resp;                     /* point at possible
  275.                                                   * response */
  276.       if (strnicmp (response, p, strlen (p)) == 0)      /* if a match,               */
  277.          return (mdm_resps[i].disp);             /* return disposition of it  */
  278.       }
  279.    return (1);                                   /* ignore all unknowns       */
  280. }
  281.  
  282.  
  283. int modem_response (ths)
  284. int ths;                                         /* millisecs to wait         */
  285. {
  286.    long end_time;                                /* holds time at end of 2min */
  287.    extern long timerset ();
  288.    char *response;                               /* pointer to modem response */
  289.    char *c, *skip_blanks ();                     /* miscellaneous pointer     */
  290.    int result = IGNORE;                          /* result code               */
  291.  
  292.    /* If this modem doesn't differentiate between RING and RINGING */
  293.    if (modemring)
  294.       mdm_resps[0].disp = 1;
  295.  
  296.    end_time = timerset (ths);                    /* arm the timeout           */
  297.    while ((result == IGNORE)                     /* until success or failure, */
  298.           && (!timeup (end_time))                /* or out of time,           */
  299.           && (!KEYPRESS ()))                     /* or user gets impatient    */
  300.       {
  301.       response = get_response (end_time);        /* get a response            */
  302.       result = parse_response (response);        /* parse, determine status   */
  303.       time_release ();
  304.       }
  305.  
  306.    if (result == SUCCESS)                        /* Got to be a CONNECT msg   */
  307.       {
  308.       if (strnicmp (response, "connect", 7) == 0)/* if this is a CONNECT,     */
  309.          {
  310.          c = skip_blanks (&response[7]);         /* get past the blanks       */
  311.          mdm_reliable[0] = '\0';                 /* Start with nothing        */
  312.          if (*c == '\0')                         /* if nothing there,         */
  313.             {
  314.             set_baud (300, 1);                   /* say that it's 300 baud    */
  315.             }
  316.          else
  317.             {
  318.             set_baud (atoi (c), 1);              /* else do baudrate fallback */
  319.             while (isdigit (*c))                 /* Get past digits           */
  320.                ++c;
  321.             c = skip_blanks (c);                 /* Get rid of blanks         */
  322.             if (*c != '\0')                      /* We have "reliable" info.  */
  323.                {
  324.                strcpy (mdm_reliable, c);         /* Copy in the info          */
  325.                }
  326.             }
  327.  
  328.          }
  329.       }
  330.  
  331.    return (result);                              /* timeout or failure or OK  */
  332. }
  333.  
  334. void mdm_cmd_string (mdm_cmd, dospace)
  335. char *mdm_cmd;
  336. int dospace;
  337. {
  338.    register char *c;
  339.  
  340.    if (mdm_cmd == NULL)                          /* defense from shit         */
  341.       return;
  342.  
  343.    for (c = mdm_cmd; *c; c++)
  344.       {
  345.       if (dospace || !isspace (*c))              /* don't output spaces       */
  346.          mdm_cmd_char (*c);                      /* output the next character */
  347.       }
  348. }
  349.  
  350. static void empty_delay ()
  351. {
  352.    long t, timerset ();
  353.  
  354.    t = timerset (500);
  355.    while ((!OUT_EMPTY ()) && (!timeup (t)))
  356.       time_release ();                           /* wait for output to finish */
  357.    if (!OUT_EMPTY ())
  358.       {
  359. /*      status_line ("!Timeout waiting for output to clear"); */
  360.       MDM_DISABLE ();
  361.       Cominit (port_ptr);
  362.       MDM_ENABLE (btypes[baud].rate);
  363.       DTR_ON ();
  364.       CLEAR_OUTBOUND ();
  365.       CLEAR_INBOUND ();
  366.       if (un_attended && fullscreen)
  367.          {
  368.          sb_dirty ();
  369.          sb_show ();
  370.          }
  371.       }
  372. }
  373.  
  374. void mdm_cmd_char (outchr)
  375. char outchr;
  376. {
  377.  
  378.    switch (outchr)
  379.       {
  380.       case '\-':                                /* if it's a dash (phone no) */
  381.          return;                                 /* ignore it                 */
  382.  
  383.       case '\|':                                /* if the CR character,      */
  384.          outchr = CR;                            /* substitute a real CR here */
  385.          break;
  386.  
  387.       case '.':                                 /* Substitute ',' for '.'    */
  388.          outchr = ',';                           /* for compatibility         */
  389.          break;
  390.  
  391.       case '\~':                                /* if the "delay" character, */
  392.          empty_delay ();                         /* wait for buffer to clear, */
  393.          timer (10);                             /* then wait 1 second        */
  394.          return;                                 /* and return                */
  395.  
  396.       case '^':                                 /* Raise DTR                 */
  397.          empty_delay ();                         /* wait for buffer to clear, */
  398.          DTR_ON ();                              /* Turn on DTR               */
  399.          return;                                 /* and return                */
  400.  
  401.       case 'v':                                 /* Lower DTR              */
  402.          empty_delay ();                         /* wait for buffer to clear, */
  403.          DTR_OFF ();                             /* Turn off DTR              */
  404.          return;                                 /* and return                */
  405.  
  406.       case '`':                                 /* Short delay              */
  407.          timer (1);                              /* short pause, .1 second    */
  408.          return;                                 /* and return                */
  409.  
  410.       default:
  411.          break;
  412.       }
  413.  
  414.    SENDBYTE (outchr);                            /* then write the character  */
  415.  
  416.    if (outchr == CR)                             /* if it was a CR,           */
  417.       {
  418.       empty_delay ();
  419.       timer (1);                                 /* allow .1 sec line quiet   */
  420.       }
  421.    else if (slowmodem)
  422.       {
  423.       timer (1);                                 /* wait .1 sec for output    */
  424.       }
  425. }
  426.  
  427. void mdm_hangup ()
  428. {
  429.  
  430.    /*
  431.     * First, if a dial command is in progress, try to get the modem to abort
  432.     * it... 
  433.     */
  434.  
  435.    CLEAR_OUTBOUND ();
  436.    CLEAR_INBOUND ();
  437.  
  438.    if (un_attended && fullscreen)
  439.       {
  440.       do_ready ("HangUp ");
  441.       }
  442.    else
  443.       {
  444.       status_line ("#Modem hang up sequence");   /* Tell what we are doing */
  445.       }
  446.  
  447.    mdm_init (modem_init);                        /* re-initialize the modem   */
  448.    timer (5);                                    /* Wait another .5 sec       */
  449.  
  450.    set_xy ("");
  451.    CLEAR_INBOUND ();                             /* then flush input and exit */
  452. }
  453.  
  454. void mdm_init (str)
  455. char *str;
  456. {
  457.    CLEAR_OUTBOUND ();
  458.    CLEAR_INBOUND ();
  459.    mdm_cmd_char (CR);                            /* output a CR, then         */
  460.    DTR_OFF ();                                   /* Drop DTR to hangup        */
  461.    timer (10);                                   /* Hold it down for 1 sec    */
  462.  
  463.    DTR_ON ();                                    /* Raise DTR,                */
  464.    timer (5);                                    /* Then hold it up for .5
  465.                                                   * sec */
  466.    set_baud (max_baud, 0);
  467.  
  468.    mdm_cmd_char (' ');                           /* output a space            */
  469.    mdm_cmd_char (CR);                            /* then another CR           */
  470.    mdm_cmd_string (str, 0);                      /* then the modem init
  471.                                                   * string */
  472.    timer (5);                                    /* Hold DTR for .5 sec more  */
  473.    CLEAR_INBOUND ();                             /* then flush input and exit */
  474. }
  475.  
  476. void send_break (t)
  477. int t;
  478. {
  479.    long t1;
  480.  
  481.    t1 = timerset (t);
  482.    do_break (1);
  483.    while (!timeup (t1))
  484.       time_release ();
  485.    do_break (0);
  486. }
  487.